<?php

namespace app\Shop\controller;

use app\BaseController;
use app\model\Commodity;
use app\model\Discount;
use app\model\Grouppurchase;
use app\model\Orders;
use app\model\Shop;
use think\console\Input;
use think\db\builder\Oracle;

class Index {
	function query() {
		//商家查询//
		return $query = Shop::select();
	}

	function commodity() {
		//商品查询//
		return $Commodity = Commodity::select();
	}

	function grouppurchase() {
		//新增团购商品
		$input = input('post.');
		$group = Grouppurchase::getBYshop_id($input['shop_id']);
		if ($group['name'] != $input['name']) {
			Grouppurchase::Insert([
				'shop_id' => $input['shop_id'],
				'name' => $input['name'],
				'commodity_id' => $input['commodity_id'],
				'commodity_name' => $input['commodity_name'],
				'groupprice' => $input['groupprice'],
				'number' => $input['number'],
				'info' => $input['info'],
			]);
			return json(['status' => true]);
		} else {
			return json(['msg' => '此命名团购商品已存在，请选择删除原团购或修改名字']);
		}

	}

	function UpdateCommodity() {
		//修改商品信息
		$input = input('post.');
		Commodity::update(
			['shop_name' => $input['shop_name'], 'price' => $input['price'], 'type' => $input['type']],
			['id' => $input['id']]);
		return json(['status' => true]);
	}

	function DelCommodity() {
		//删除商品
		$input = input('post.');
		Commodity::where(['id' => $input['id']])->delete();
		return json(['status' => true]);
	}

	function ReleaseDiscount() {
		//发布优惠
		$input = input('post.');
		Discount::Insert([
			'shop_id' => $input['shop_id'],
			'value' => $input['value'],
			'num' => $input['num'],
			'commodity_id' => $input['commodity_id'],
			'commodity_name' => $input['commodity_name'],
			'info' => $input['info'],
		]);
		return json(['status' => true]);
	}

	function ViewOrder() {
		//查看商家订单
		$input = input('post.');
		$data3 = Orders::where(['id' => $input['id'], 'state' => '未接单'])->with('order')->select();
		$data4 = Orders::Where( ['id' => $input['id'], 'state' => '已接单'])->with('order')->select();
		$data2 = Orders::Where(['id' => $input['id'], 'state' => '已完成'])->with('order')->select();
		return json(['Accepted' => $data4, 'Completed' => $data2, 'NotAccepted' => $data3]);

	}
	function state_update(){
		$input = input('post.');
		Orders::where(['id'=>$input['id']])->update(['state'=>'已接单']);
		$data4 = Orders::Where( ['id' => $input['id'], 'state' => '已接单'])->with('order')->select();
		$data2 = Orders::Where(['id' => $input['id'], 'state' => '已完成'])->with('order')->select();
		$data3 = Orders::where(['id' => $input['id'], 'state' => '未接单'])->with('order')->select();
		return json(['Accepted' => $data4, 'Completed' => $data2, 'NotAccepted' => $data3]);
	}
	function state_update_NotAccepted(){
		$input = input('post.');
		Orders::where(['id'=>$input['id']])->update(['state'=>'已完成']);
		$data2 = Orders::Where(['id' => $input['id'], 'state' => '已完成'])->with('order')->select();
		$data3 = Orders::where(['id' => $input['id'], 'state' => '未接单'])->with('order')->select();
		$data4 = Orders::Where( ['id' => $input['id'], 'state' => '已接单'])->with('order')->select();
		return json(['Accepted' => $data4, 'Completed' => $data2, 'NotAccepted' => $data3]);
	}
	function CommodityInsert() {
		$input = input('post.');
		try {
			$a = Commodity::getByshop_id($input['shop_id']);
			if ($a['commodity_name'] != $input['commodity_name']) {
				Commodity::Insert([
					'shop_id' => $input['shop_id'],
					'commodity_name' => $input['commodity_name'],
					'commodity_info' => $input['commodity_info'],
					'price' => $input['price'],
					'type' => $input['type'],
				]);
			}
			return json(['state' => true]);

		} catch (customException $e) {
		return	$e->errorMessage('商品重复');
		};
	}

	function upload() {
		#上传商品图片
		$input = input('post.');
		if ($_FILES["file"]["error"]) {
			echo $_FILES["file"]["erroe"];
		} else {
			//控制上传的文件类型，大小
			if ($_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/png" && $_FILES["file"]["size"] < 2048000) {
				preg_match("/\/(.*)/", $_FILES["file"]["type"], $checkUrl);
				$name = time();
				Commodity::where(['shop_id' => $input['shop_id'], 'commodity_name' => $input['img_name']])->update(['commodity_img' => $name . "." . $checkUrl['1']]);
				$dir = "static/menu" . $input['shop_id'];
				$filename = $dir . "/" . $name . "." . $checkUrl['1'];
				if (!file_exists($dir)) {
					mkdir($dir, 0777);
				}
				move_uploaded_file($_FILES["file"]["tmp_name"], $filename);
				return json(['state' => true, 'msg' => "上传成功！"]);
			} else {
				return json(['state' => false, 'msg' => "文件类型不正确！"]);
			}
		}
	}

	function upload_menu() {
		#上传商户头像
		$input = input('post.');
		if ($_FILES["file"]["error"]) {
			echo $_FILES["file"]["erroe"];
		} else {
			//控制上传的文件类型，大小
			if ($_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/png" && $_FILES["file"]["size"] < 2048000) {
				preg_match("/\/(.*)/", $_FILES["file"]["type"], $checkUrl);
				$pho="static/shop_img/";
				$filename = "static/shop_img/" . $input['img_name'] . "." . $checkUrl['1'];
				if (!file_exists($pho)){
					mkdir($pho,0777);
				}
				move_uploaded_file($_FILES["file"]["tmp_name"], $filename);
				Shop::where(['Shoplogin_id' => $input['img_id']])->update(['shop_img' => $input['img_name'] . "." . $checkUrl['1']]);
				return json(['state' => true, 'msg' => "上传成功！"]);
			} else {
				return json(['state' => false, 'msg' => "文件类型不正确！"]);
			}
		}
	}

	function details($id) {
		return Shop::Where(['shoplogin_id' => $id])->select();
	}

	function update_shop() {
		$input = input('post.');
		Shop::update(
			['name' => $input['name'], 'address' => $input['address'], 'type' => $input['type'], 'phone' => $input['phone']],
			['id' => $input['id']]);
		return json(['status' => true]);
	}


}

;
